package soa.security.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.ldap.query.ContainerCriteria;
import org.springframework.ldap.query.SearchScope;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.stereotype.Repository;

import soa.security.dao.ApplsystemDao;
import soa.security.dao.impl.SpringLdapDaoImpl.BaseContextMapper;
import soa.security.domain.Applsystem;
import soa.security.domain.Department;
import soa.security.query.Convertor;

/**
 * 
 * @author CY
 *
 */
@Repository
public class ApplsystemDaoImpl extends SpringLdapDaoImpl<Applsystem> implements ApplsystemDao {
	
	@Override
	public void create(Applsystem t) {
		t.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		ldapTemplate.create(t);
	}
	
	@Override
	public Applsystem findByDn(String dn) {
		// TODO Auto-generated method stub
		Applsystem o = ldapTemplate.findByDn(LdapNameBuilder.newInstance(dn)
				.build(), Applsystem.class);
		o.setBid(LdapUtils.getStringValue(o.getDn(), o.getDn().size() - 1));
		o.setOrganization(unitBelongs(dn));
		return o;
	}

	@Override
	public String getObjectClass() {
		// TODO Auto-generated method stub
		return "zjtzsw-applsystem";
	}

	@Override
	public List<Applsystem> allApplsystems(String baseDn, SearchScope searchScope, Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		ContainerCriteria query = baseQuery(baseDn, searchScope);

		buildLikeCriteria(query, parameters, "sname");

		return ldapTemplate.search(query, new BaseContextMapper());
	}

}
